#coding=utf-8
#Boa:Dialog:DlgPeer

import wx

from MyListCtrl import MyListCtrlPeer
from product import CDataPeer

def create(parent):
    return DlgPeer(parent)

[wxID_DLGPEER, wxID_DLGPEERBTNDEL, wxID_DLGPEERBTNNEW, wxID_DLGPEERBTNSEARCH, 
 wxID_DLGPEERSTATICTEXT1, wxID_DLGPEERTCQUERY, 
] = [wx.NewId() for _init_ctrls in range(6)]

class DlgPeer(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPEER, name=u'DlgPeer', parent=prnt,
              pos=wx.Point(410, 118), size=wx.Size(807, 652),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u4f9b\u8d27\u5546')
        self.SetClientSize(wx.Size(799, 618))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.staticText1 = wx.StaticText(id=wxID_DLGPEERSTATICTEXT1,
              label=u'\u67e5\u8be2\uff1a', name='staticText1', parent=self,
              pos=wx.Point(24, 16), size=wx.Size(51, 16), style=0)

        self.tcQuery = wx.TextCtrl(id=wxID_DLGPEERTCQUERY, name=u'tcQuery',
              parent=self, pos=wx.Point(80, 8), size=wx.Size(192, 26), style=0,
              value=u'')

        self.btnSearch = wx.Button(id=wxID_DLGPEERBTNSEARCH,
              label=u'\u67e5\u627e', name=u'btnSearch', parent=self,
              pos=wx.Point(280, 8), size=wx.Size(93, 26), style=0)

        self.btnNew = wx.Button(id=wxID_DLGPEERBTNNEW, label=u'\u65b0\u589e',
              name=u'btnNew', parent=self, pos=wx.Point(24, 48),
              size=wx.Size(93, 26), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGPEERBTNNEW)

        self.btnDel = wx.Button(id=wxID_DLGPEERBTNDEL, label=u'\u5220\u9664',
              name=u'btnDel', parent=self, pos=wx.Point(136, 48),
              size=wx.Size(93, 26), style=0)
        self.btnDel.Bind(wx.EVT_BUTTON, self.OnBtnDelButton,
              id=wxID_DLGPEERBTNDEL)

    def __init__(self, parent, popup=False):
        self._init_ctrls(parent)
        
        self.popup = popup
        
        self.myInit()
        
    def myInit(self):
        self.m_peer = None
        
        self.listCtrl = MyListCtrlPeer(self)
        self.listCtrl.SetSize(wx.Size(750, 450))
        self.listCtrl.Move(wx.Point(25, 80))
        self.listCtrl.refresh()
        
        if (self.popup):
            self.listCtrl.Bind(wx.EVT_LEFT_DCLICK, self.OnSelectItem)

    def OnSelectItem(self, event):
        self.m_peer = self.listCtrl.getSelectObj()
        if self.m_peer is None:
            return
        self.EndModal(wx.ID_OK)
            
    def OnBtnNewButton(self, event):
        import DlgPeerEditor
        
        dlg = DlgPeerEditor.create(self)
        if (dlg.ShowModal() == wx.ID_OK):
            if (self.popup):
                self.m_peer = dlg.m_peer
                self.EndModal(wx.ID_OK)
            else:
                self.listCtrl.refresh()

    def OnBtnDelButton(self, event):
        msg = u'删除之前请先确认所有与该项相关的数据均已删除或取消关系？'
        dlg = wx.MessageDialog(self, msg,u'提示',wx.YES_NO|wx.ICON_QUESTION)
        if(dlg.ShowModal() == wx.ID_NO):
            return
        
        obj = self.listCtrl.getSelectObj()
        if obj is None:
            return
        
        ret = obj.delete()
        
        self.listCtrl.refresh()
        
        print 'try to delete peer. return %d' %ret
